home *** CD-ROM | disk | FTP | other *** search
/ Fritz: All Fritz / All Fritz.zip / All Fritz / FILES / PROGMISC / FPCDOCS.LZH / SAVEREST.SEQ < prev    next >
Text File  |  1988-09-20  |  2KB  |  80 lines

  1. \ SAVEREST.SEQ  Save and restore words for variables or values.
  2.  
  3. FILES DEFINITIONS
  4.  
  5. VARIABLE SAVEREST.SEQ
  6.  
  7. FORTH DEFINITIONS   META IN-META
  8.  
  9. comment:
  10.  
  11.         These words allow you to quickly save the contents of a
  12.         variable of value on the return stack, and set it to some other
  13.         value temporarily with SAVE!>.  The previous contents may then
  14.         be restored with RESTORE>.
  15.  
  16.         Here is an example, first the old way:
  17.  
  18.                 VARIABLE MYSTUFF
  19.  
  20.                 : DEF   ( --- )
  21.                         32 MYSTUFF DUP @ >R !
  22.                         ... Do something with the new MYSTUFF ...
  23.                         R> MYSTUFF ! ;
  24.  
  25.         Here is an example of how to use SAVE!> and RESTORE>:
  26.  
  27.                 : DEF   ( --- )
  28.                         32 SAVE!> MYSTUFF
  29.                         ... Do something with the new MYSTUFF ...
  30.                         RESTORE> MYSTUFF ;
  31.  
  32. comment;
  33.  
  34. CODE %SAVE!>    ( N1 --- )
  35.                 LODSW ES:
  36.                 MOV BX, AX
  37.                 ADD BX, # 3
  38.                 SUB RP, # 2
  39.                 MOV AX, 0 [BX]
  40.                 MOV 0 [RP], AX
  41.                 POP 0 [BX]
  42.                 NEXT            END-CODE
  43.  
  44. CODE %SAVE>     ( --- )
  45.                 LODSW ES:
  46.                 MOV BX, AX
  47.                 SUB RP, # 2
  48.                 MOV AX, 3 [BX]
  49.                 MOV 0 [RP], AX
  50.                 NEXT            END-CODE
  51.  
  52. CODE %RESTORE>  ( --- )
  53.                 PUSH 0 [RP]
  54.                 ADD RP, # 2
  55.                 LODSW ES:
  56.                 MOV BX, AX
  57.                 POP 3 [BX]
  58.                 NEXT            END-CODE
  59.  
  60. : ?COMP         ( --- )
  61.                 STATE @ 0= ABORT" Use only while compiling" ;
  62.  
  63.  
  64. \ the following word does the equivalent of "VARIABLE-NAME DUP @ >R !"
  65.  
  66.                                 \ Leaves value in T1 on return stack
  67. : SAVE!>        ( N1 T1 --- )   \ and sets body of t1 to n1.
  68.                 ?COMP COMPILE %SAVE!> ; IMMEDIATE
  69.  
  70. \ This one does "VARIABLE-NAME @ >R"
  71.  
  72. : SAVE>         ( T1 --- )      \ Saves value in T1 body on return stack.
  73.                 ?COMP COMPILE %SAVE> ; IMMEDIATE
  74.  
  75. \ This does "R> VARIABLE-NAME !"
  76.  
  77. : RESTORE>      ( T1 --- )      \ Restore body of T1 from return stack.
  78.                 ?COMP COMPILE %RESTORE> ; IMMEDIATE
  79.  
  80.